<!DOCTYPE html>
<html lang="en-us" id="extr-page">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../global/css/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/layui/css/layui.css">
    <link rel="stylesheet" href="../../global/css/ztree/demo.css" type="text/css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form"  enctype="multipart/form-data">
        <fieldset>
            <input type="hidden" id="id" name="id">
            <div class="form-group">
                <label class="col-md-2 control-label">消息标题</label>
                <div class="col-md-10">
                    <input type="text" class="form-control" placeholder="消息标题" name="messageTitle" id="messageTitle" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">推送用户</label>
                <div class="col-md-10">
                    会员:<input type="checkbox"  value="0" name="pushUserTypeList" />    &nbsp&nbsp&nbsp
                    导购:<input type="checkbox"  value="1" name="pushUserTypeList" />    &nbsp&nbsp&nbsp
                    讲师:<input type="checkbox"  value="2" name="pushUserTypeList" />    &nbsp&nbsp&nbsp
                    经理:<input type="checkbox"  value="3" name="pushUserTypeList" />    &nbsp&nbsp&nbsp
                    商家:<input type="checkbox"  value="4" name="pushUserTypeList" />    
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">消息模板</label>
                <div class="col-md-10">
                    <select class="form-control" onchange=wxChange() name="wxFormworkId" id="wxFormworkId" >
                    	<option value="">请选择</option>
                    </select>
                </div>
            </div>
             <div class="form-group">
                <label class="col-md-2 control-label">消息内容</label>
                <div class="col-md-10" id="content1">
                    <textarea class="form-control" name="content" id="content" ></textarea>
                </div>
            </div>
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='messagePushList.html'">返回</button>
                        <button  class="btn btn-primary" type="button" id="save">
                            <i class="fa fa-save"></i> 保存草稿
                        </button>
                        <button  class="btn btn-primary" type="button" id="send">
                            <i class="fa fa-save"></i> 立即推送
                        </button>
                    </div>
                </div>
            </div>

        </fieldset>
    </form>
</div>

<script type="text/javascript" src="../../global/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../global/js/jq.js"></script>
<script type="text/javascript" src="../../global/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../global/js/common.js"></script>
<script type="text/javascript" src="../../global/layui/layui.js"></script>
<script type="text/javascript" src="../../global/js/libs/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="../../global/js/ajaxfileupload.js"></script>
<script type="text/javascript" src="../../global/js/my/ztree-menu.js"></script>
<script type="text/javascript">
layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#beginTime'
            ,type: 'datetime'
                ,format: 'yyyy-MM-dd HH:mm:ss'
        });
    });
    //获取消息模板
    $.ajax({
        type: 'get',
        url: '/clyjMessagePushFormwork/queryMessagePushFormwork',
        data:{id:id},
        contentType: "application/json; charset=utf-8",
        async: false,
        success: function (data) {
        	console.info(data);
        	for(var i=0;i<data.length;i++){
        		$("#wxFormworkId").append("<option value='"+data[i].wxFormworkId+"'>"+data[i].wxFormworkName+"</option>");
        	}
        }
    });
    initData();
    var id = getUrlParam("id");
    function initData() {
        if (id != "" && id!=null) {
            $.ajax({
                type: 'get',
                url: '/clyjMessagePush/' + id,
                async: false,
                success: function (data) {
                    $("#messageTitle").val(data.messageTitle);
                    var type = data.pushUserType;
                    if(type!=null && type!=""){
                    	var boxObj = $("input:checkbox[name='pushUserTypeList']");  //获取所有的复选框
	                    var express = type.split(",");
	                    for(i=0;i<boxObj.length;i++){
                   	       for(j=0;j<express.length;j++){            
                   	           if(boxObj[i].value == express[j])  //如果值与修改前的值相等
                   	           {
                   	               boxObj[i].checked= true;
                   	               break;
                   	           }
                   	       }
                   	    }
                    }
                    $("#wxFormworkId").val(data.wxFormworkId);
                    $("#content").val(data.content);
                }
            });
        }
    }


    $('#form').bootstrapValidator();
	//保存草稿
    $("#save").click(function(){
    	var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }
        $("form").ajaxSubmit({
			 type: 'post',
            url: '/clyjMessagePush/messagePush',
            data:{"state":"1"},
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
            	if(data.code=="success"){
                   layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                       location.href = "messagePushList.html";
                   });
               }
            	else{
            		layer.msg(data.msg);
            	}
            }
		})
    })
    //新增消息
    $("#send").click(function(){
    	
    	var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }
        $("form").ajaxSubmit({
			 type: 'post',
            url: '/clyjMessagePush/messagePush',
            data:{"state":"0"},
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                   layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                       location.href = "messagePushList.html";
                   });
               }
		})
    })
    
    //消息模板改变事件
    function wxChange(){
		var wxId = $("#wxFormworkId").val();
		//根据模板id查询明细
		$.ajax({
			url:"/clyjMessagePushFormwordDesc/queryMessagePushFormwordDesc",
			data:{"wxFormworkId":wxId},
			type:"post",
			dataType:"json",
			success:function(data){
				console.info(data);
				//显示到内容框
				var list = data.data;
				if(list==null || list.length==0)
					return;
				$("#content1").empty();
				var content="";
				for(var i=0;i<list.length;i++){
					$("#content1").append(list[i].wxDesc+':<input class="form-control"  type="text" name="value" /><input type="hidden" name="wxCode" value="'+list[i].wxCode+'" />');
				}
			}
		})
	}

</script>
</body>
</html>